import Vue from 'vue'
import VueRouter from 'vue-router'
//引入login组件
import Login from '../components/Login.vue'
//引入Hone组件
import Home from '../components/Home.vue'
//引入Welcome组件
import Welcome from '../components/Welcome.vue'
//引入UserList组件
import UserList from '../components/admin/UserList.vue'
import RightsList from '../components/admin/RightsList.vue'
import SportList from '../components/admin/SportList.vue'
import GoodsList from '../components/admin/GoodsList.vue'

import IntroductionList from '../components/use/IntroductionList.vue'
import CaloriesList from '../components/use/CaloriesList.vue'
import FoodList from '../components/use/FoodList.vue'

Vue.use(VueRouter)

  const routes = [ 
    {
      path: "/",
      redirect:"/login"
    }, {
      path:"/login",
      component:Login
    },{
		path:"/home",
		component:Home,//!!!component注意大小写
		redirect:"/welcome",
		children:[
			{path:"/welcome",component:Welcome,},
			{path:"/user",component:UserList,},
			{path:"/rights",component:RightsList,},
			{path:"/sport",component:SportList,},
			{path:"/goods",component:GoodsList,},
			{path:"/Introduction",component:IntroductionList,},
			{path:"/calories",component:CaloriesList,},
			{path:"/food",component:FoodList,}
		]
	}
]

  const router = new VueRouter({
    routes
})

//出现问题的时候使用
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location,onResolve,onReject){
	 if(onResolve || onReject) return originalPush.call(this,location,onResolve,onReject)
	 return originalPush.call(this,location).catch(err => err)
}

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
	//to将要访问
	//from从哪访问
	//next接着干的事next(url) 重定向到url上 next()继续访问to的路径
	if(to.path=='/login') return next();
	//获取user
	const userFlag =  window.sessionStorage.getItem("user");//取出当前用户
	if(!userFlag) return next('/login');//无值,返回登录页
	next();//符合要求,放行
})

export default router//暴露出去
